//
//  ProjectProblem.m
//  nova
//
//  Created by user on 11/06/13.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "ProjectProblem.h"


@implementation ProjectProblem
@synthesize problem;
// The designated initializer.  Override if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.
/*
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization.
    }
    return self;
}
*/

-(IBAction) saveProblem{	
	DataContext *context = [[DataContext alloc] init];
	if([problem isNew] && [problem changed]){
			[context executeQuery:[problem getInsertString]];
			[[myAppDelegate getProject] insertNewProblem:problem];
	}else if([problem changed]){
		[context executeQuery:[problem getUpdateString]];
	}
	//[self.view removeFromSuperview];
	[context dealloc];
	//[action dealloc];
	//action = nil;
	context = nil;
}

-(void) alertView : (UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
	//UIAlertView *exitingWithSave = [[UIAlertView alloc] initWithTitle:@"ZAnother?" message:@"Another?" delegate:self cancelButtonTitle:@"No"  otherButtonTitles:nil];
	//[exitingWithSave addButtonWithTitle:@"Yes"];
	//[exitingWithSave show];
	if(buttonIndex == 1)
	{
		DataContext *context = [[DataContext alloc] init];
		if([problem isNew] && [problem changed]){
			@try {
				[context executeQuery:[problem getInsertString]];
				[[myAppDelegate getProject] insertNewProblem:problem];
			}
			@catch (NSException * e) {
				UIAlertView *exitingWithSave = [[UIAlertView alloc] initWithTitle:@"Error saving to database." message:@"There was an error saving this action. Please restart the aplication!" delegate:nil cancelButtonTitle:@"Ok"  otherButtonTitles:nil];
			}
		}else if([problem changed]){
			[context executeQuery:[problem getUpdateString]];
		}
		//[self.view removeFromSuperview];
		[context dealloc];
		//[action dealloc];
		//action = nil;
		context = nil;		
	}
}


// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
    [super viewDidLoad];
	[scroll
	 setContentSize:CGSizeMake(320,
							   1106)];
	[self initObjects];
	
	backgroundView.backgroundColor = [[UIColor alloc] initWithPatternImage:[UIImage imageNamed:@"background.jpg"]];
}

-(IBAction) openDetails{
	
}

-(IBAction) openComments{
	ProjectCommentList* commentList = [[ProjectCommentList alloc] initWithNibName:@"ProjectCommentList" bundle:[NSBundle mainBundle]];
	[commentList setProblem:problem];
	[commentList setCommentList : [problem commentList]];
	[self.view addSubview : commentList.view];	
}

-(IBAction) openHistory{
}

-(IBAction) btStatusClick : (id) sender{
	PickerView *statusPicker = [[PickerView alloc] initWithNibName:@"PickerView" bundle:[NSBundle mainBundle]];
	[self.view addSubview:statusPicker.view];
}

-(IBAction) btResponseFinish : (id) sender{
	DatePicker *datePicker = [[DatePicker alloc] initWithNibName:@"DatePicker" bundle:[NSBundle mainBundle]];
	[datePicker setTargetObject:problem];
	[datePicker setTargetTimeSelector:[problem getSetResponseFinishSelector]];
	[datePicker setTargetButton:(UIButton *)sender];
	[self.view addSubview:datePicker.view];
}

-(IBAction) textFieldReturn : (id) sender{
	[problem setProblemDescription:[[txtDescription text] retain]];
	[problem setImpact_Description:[[txtImpactDescription text] retain]];
	[problem setResponse_Description:[[txtResponseDescription text] retain]];
	
	[txtDescription resignFirstResponder];
	[txtImpactDescription resignFirstResponder];
	[txtResponseDescription resignFirstResponder];
}

-(void) initObjects{
	[lblProject setText: [[myAppDelegate getProject] getProjectName]];
	[lblDateCreated setText:[Library getLocalizedDateString : [problem getDateTimeCreated]]];
	if(![problem isNew]){
		[btResponseOwner setTitle: [problem getResponseOwner] forState: UIControlStateNormal ];
		[txtDescription setText: [problem getProblemDescription]];
		[txtImpactDescription setText:[problem getImpact_Description]];
		[txtResponseDescription setText:[problem getResponse_Description]];
		[btResponseFinish setTitle:[Library getLocalizedDateString : [problem getResponse_Finish]] forState: UIControlStateNormal ];
		[btState setTitle:[[problem problemState] getDescription] forState: UIControlStateNormal];
	}
}


/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations.
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/

- (void)didReceiveMemoryWarning {
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc. that aren't in use.
}

- (void)viewDidUnload {
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}


- (void)viewWillDisappear:(BOOL)animated {
    [self saveProblem];
	[super viewWillDisappear:animated];
	
}

- (void)dealloc {
    [super dealloc];
}


@end
